home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / spheremap / GLSMAP / fakeraytrace.c next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  17.4 KB  |  723 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1998.  */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* fakeraytrace.c - two reflective objects, one has reflection of other's reflection */
  9.  
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <GL/glut.h>
  14. #include <GL/glsmap.h>
  15.  
  16. #if defined(GL_EXT_texture_object) && !defined(GL_VERSION_1_1)
  17. #define glBindTexture(A,B)     glBindTextureEXT(A,B)
  18. #endif
  19.  
  20. typedef struct {
  21.         GLfloat *obj2draw;
  22.         int drawObj1;
  23.         GLuint texobj;
  24. } Object;
  25.  
  26. enum { X, Y, Z };
  27.  
  28. enum {
  29.     TO_NONE = 0,
  30.     TO_FRONT, TO_TOP, TO_BOTTOM, TO_LEFT, TO_RIGHT,
  31.     TO_BACK,
  32.     TO_SPHERE_MAP,
  33.         TO_FRONT2, TO_TOP2, TO_BOTTOM2, TO_LEFT2, TO_RIGHT2,
  34.     TO_BACK2,
  35.     TO_SPHERE_MAP2,
  36.  
  37.         TO_FRONT3, TO_TOP3, TO_BOTTOM3, TO_LEFT3, TO_RIGHT3,
  38.     TO_BACK3,
  39.     TO_SPHERE_MAP3
  40. };
  41.  
  42. static GLuint texobjs[6] = {
  43.     TO_FRONT, TO_TOP, TO_BOTTOM,
  44.     TO_LEFT, TO_RIGHT, TO_BACK
  45. };
  46.  
  47. static GLuint texobjs2[6] = {
  48.     TO_FRONT2, TO_TOP2, TO_BOTTOM2,
  49.     TO_LEFT2, TO_RIGHT2, TO_BACK2
  50. };
  51.  
  52. static GLuint texobjs3[6] = {
  53.     TO_FRONT3, TO_TOP3, TO_BOTTOM3,
  54.     TO_LEFT3, TO_RIGHT3, TO_BACK3
  55. };
  56.  
  57. int moving = 0;
  58. int multibounce = 1;
  59. SphereMap *smap, *smap2, *smap3;
  60. int win;
  61.  
  62. GLfloat up[3] = { 0, 1, 0 };
  63. GLfloat up2[3] = { 0, 0, 1 };
  64. GLfloat eye[3] = { 0, 0, -15 };
  65. GLfloat obj[3] = { 0, 4, 0 };
  66. GLfloat obj2[3] = { 0, -4, 0 };
  67.  
  68. Object sphere[] = {
  69.         { obj, 0, TO_SPHERE_MAP },
  70.         { obj2, 1, TO_SPHERE_MAP2 },
  71.         { obj, 0, TO_SPHERE_MAP3 } };
  72.  
  73. GLfloat timeCount;
  74. int W, H;
  75.  
  76. int showSphereMap = 0;
  77. int object = 0;
  78. int texdim = 64;
  79. int doubleBuffer = 1;
  80. int dynamicSmap = 1;
  81. int cullBackFaces = 1;
  82. int doSphereMap = 1;
  83. int multipass = 0;
  84. int animation = 0;
  85. int sphereTess = 20;
  86.  
  87. static char *pattern[] = {
  88.   "......xxxx......",
  89.   "......xxxx......",
  90.   "......xxxx......",
  91.   "......xxxx......",
  92.   "......xxxx......",
  93.   "......xxxx......",
  94.   "xxxxxxxxxxxxxxxx",
  95.   "xxxxxxxxxxxxxxxx",
  96.   "xxxxxxxxxxxxxxxx",
  97.   "xxxxxxxxxxxxxxxx",
  98.   "......xxxx......",
  99.   "......xxxx......",
  100.   "......xxxx......",
  101.   "......xxxx......",
  102.   "......xxxx......",
  103.   "......xxxx......",
  104. };
  105.  
  106. static void
  107. makePatternTexture(void)
  108. {
  109.   GLubyte patternTexture[16][16][4];
  110.   GLubyte *loc;
  111.   int s, t;
  112.  
  113.   /* Setup RGB image for the texture. */
  114.   loc = (GLubyte*) patternTexture;
  115.   for (t = 0; t < 16; t++) {
  116.     for (s = 0; s < 16; s++) {
  117.       if (pattern[t][s] == 'x') {
  118.     /* Nice green. */
  119.         loc[0] = 0x1f;
  120.         loc[1] = 0x8f;
  121.         loc[2] = 0x1f;
  122.         loc[3] = 0x7f;  /* opaque */
  123.       } else {
  124.     /* Light gray. */
  125.         loc[0] = 0x00;
  126.         loc[1] = 0x00;
  127.         loc[2] = 0x00;
  128.         loc[3] = 0x00;  /* transparent */
  129.       }
  130.       loc += 4;
  131.     }
  132.   }
  133.  
  134.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  135.  
  136.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  137.     GL_LINEAR_MIPMAP_LINEAR);
  138.   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 16, 16,
  139.     GL_RGBA, GL_UNSIGNED_BYTE, patternTexture);
  140. }
  141.  
  142. void
  143. reshape(int w, int h)
  144. {
  145.     W = w;
  146.     H = h;
  147. }
  148.  
  149. void
  150. positionLights(int view, void *context)
  151. {
  152.     static GLfloat light1Pos[4] = { -41.0, 41.0, -41.0, 1.0 };
  153.     static GLfloat light2Pos[4] = { +41.0, 0.0, -41.0, 1.0 };
  154.     static GLfloat light3Pos[4] = { -41.0, 0.0, +41.0, 1.0 };
  155.     static GLfloat light4Pos[4] = { +41.0, 0.0, +41.0, 1.0 };
  156.  
  157.     glLightfv(GL_LIGHT0, GL_POSITION, light1Pos);
  158.     glLightfv(GL_LIGHT1, GL_POSITION, light2Pos);
  159.     glLightfv(GL_LIGHT2, GL_POSITION, light3Pos);
  160.     glLightfv(GL_LIGHT3, GL_POSITION, light4Pos);
  161. }
  162.  
  163. extern void drawObject(Object *it);
  164.  
  165. void
  166. drawView(int view, void *context)
  167. {
  168.         Object *it = context;
  169.  
  170.         /* right green small cube (+5,0,-8) */
  171.     glPushMatrix();
  172.       glTranslatef(5.0, 0.0, -8.0);
  173.       glRotatef(-45, 1.0, 0.0, 1.0);
  174.       glColor3f(0.0, 1.0, 0.0);
  175.       glutSolidCube(2.0);
  176.     glPopMatrix();
  177.     /* left red cube (-5,0,-8) */
  178.     glPushMatrix();
  179.       glTranslatef(-5.0, 0.0, -8.0);
  180.       glRotatef(45, 1.0, 0.0, 1.0);
  181.       glColor3f(1.0, 0.0, 0.0);
  182.       glutSolidCube(6.0);
  183.     glPopMatrix();
  184.     /* left blue cube (-7,0,0); */
  185.     glPushMatrix();
  186.       glTranslatef(-7.0, 0.0, 0.0);
  187.       glColor3f(0.0, 0.0, 1.0);
  188.       glutSolidCube(5.0);
  189.     glPopMatrix();
  190.     /* right cyan big cube (+7,0,0) */
  191.     glPushMatrix();
  192.       glTranslatef(7.0, 0.0, 0.0);
  193.       glRotatef(30, 1.0, 1.0, 0.0);
  194.       glColor3f(0.0, 1.0, 1.0);
  195.       glutSolidCube(5.0);
  196.     glPopMatrix();
  197.     /* distant yellow sphere (0,0,+10) */
  198.     glPushMatrix();
  199.       glTranslatef(0.0, 0.0, 10.0);
  200.       glColor3f(1.0, 1.0, 0.0);
  201.       glutSolidSphere(7.0, 8, 8);
  202.     glPopMatrix();
  203.  
  204.         if (it && multibounce) {
  205.                 if (it->drawObj1) {
  206.                         glEnable(GL_TEXTURE_2D);
  207.                         drawObject(&sphere[2]);
  208.                         glDisable(GL_TEXTURE_2D);
  209.                 }
  210.         }
  211. }
  212.  
  213. void
  214. drawObject(Object *it)
  215. {
  216.         static GLfloat xplane[4] = { 1, 0, 0, 0 };
  217.         static GLfloat zplane[4] = { 0, 1, 0, 0 };
  218.         GLfloat *obj2draw = it->obj2draw;
  219.  
  220.         if (!cullBackFaces) {
  221.                 glEnable(GL_CULL_FACE);
  222.                 glCullFace(GL_FRONT);
  223.         }
  224.  
  225.     glPushMatrix();
  226.  
  227.                 glTranslatef(obj2draw[X], obj2draw[Y], obj2draw[Z]);
  228.  
  229.                 if (doSphereMap) {
  230.                 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  231.                 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  232.                 glEnable(GL_TEXTURE_GEN_S);
  233.                 glEnable(GL_TEXTURE_GEN_T);
  234.                 glEnable(GL_TEXTURE_2D);
  235.                 glBindTexture(GL_TEXTURE_2D, it->texobj);
  236.                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
  237.             GL_REPLACE);
  238.                 } else {
  239.                         glTexGenfv(GL_S, GL_OBJECT_PLANE, xplane);
  240.                         glTexGenfv(GL_T, GL_OBJECT_PLANE, zplane);
  241.                         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  242.                         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  243.                         glEnable(GL_TEXTURE_GEN_S);
  244.                 glEnable(GL_TEXTURE_GEN_T);
  245.                 glEnable(GL_TEXTURE_2D);
  246.                         glBindTexture(GL_TEXTURE_2D, 100);
  247.                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
  248.             GL_REPLACE);
  249.                 }
  250.  
  251.         glColor3f(1.0, 1.0, 1.0);
  252.         switch (object) {
  253.         case 0:
  254.                         glutSolidSphere(3.5, 20, 20);
  255.             break;
  256.         case 1:
  257.             glScalef(3.0, 3.0, 3.0);
  258.             glRotatef(30.0, 1.0, 1.0, 0.0);
  259.             glutSolidIcosahedron();
  260.             break;
  261.         case 2:
  262.             glFrontFace(GL_CW);
  263.             glutSolidTeapot(3.0);
  264.             glFrontFace(GL_CCW);
  265.             break;
  266.         }
  267.  
  268.             glDisable(GL_TEXTURE_GEN_S);
  269.                glDisable(GL_TEXTURE_GEN_T);
  270.     glPopMatrix();
  271.  
  272.         if (!cullBackFaces) {
  273.                 glCullFace(GL_BACK);
  274.         }
  275. }
  276.  
  277. void
  278. vsub(const GLfloat *src1, const GLfloat *src2, GLfloat *dst)
  279. {
  280.     dst[0] = src1[0] - src2[0];
  281.     dst[1] = src1[1] - src2[1];
  282.     dst[2] = src1[2] - src2[2];
  283. }
  284.  
  285. void
  286. vcopy(const GLfloat *v1, GLfloat *v2)
  287. {
  288.     register int i;
  289.     for (i = 0 ; i < 3 ; i++)
  290.         v2[i] = v1[i];
  291. }
  292.  
  293. void
  294. vcross(const GLfloat *v1, const GLfloat *v2, GLfloat *cross)
  295. {
  296.     GLfloat temp[3];
  297.  
  298.     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  299.     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  300.     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  301.     vcopy(temp, cross);
  302. }
  303.  
  304. void
  305. display(void)
  306. {
  307.         GLfloat tmp1[3], tmp2[3];
  308.  
  309.     glDisable(GL_TEXTURE_2D);
  310.     glEnable(GL_DEPTH_TEST);
  311.            glEnable(GL_CULL_FACE);
  312.  
  313.         if (dynamicSmap) {
  314.                 if (multibounce) {
  315.                 glDisable(GL_TEXTURE_2D);
  316.             glEnable(GL_DEPTH_TEST);
  317.                 glEnable(GL_CULL_FACE);
  318.  
  319.                 vsub(eye,obj,tmp1);
  320.                 vsub(obj2,obj,tmp2);
  321.                 vcross(tmp1,tmp2,up2);
  322.                 vcross(up2,tmp2,up2);
  323.  
  324.                 smapSetUpVector(smap3, up2);
  325.             smapGenSphereMap(smap3);
  326.                 }
  327.         
  328.                 glDisable(GL_TEXTURE_2D);
  329.             glEnable(GL_DEPTH_TEST);
  330.                 glEnable(GL_CULL_FACE);
  331.             smapGenSphereMap(smap);
  332.  
  333.                 glDisable(GL_TEXTURE_2D);
  334.             glEnable(GL_DEPTH_TEST);
  335.                 glEnable(GL_CULL_FACE);
  336.             smapGenSphereMap(smap2);
  337.         }
  338.  
  339.     /* smapGenSphereMap leaves scissor enabled, disable it. */
  340.     glDisable(GL_SCISSOR_TEST);
  341.  
  342.     glViewport(0, 0, W, H);
  343.  
  344.     if (showSphereMap) {
  345.         glClear(GL_COLOR_BUFFER_BIT);
  346.  
  347.         glMatrixMode(GL_PROJECTION);
  348.         glLoadIdentity();
  349.         gluOrtho2D(0, 1, 0, 1);
  350.         glMatrixMode(GL_MODELVIEW);
  351.         glLoadIdentity();
  352.  
  353.         glEnable(GL_TEXTURE_2D);
  354.         glDisable(GL_DEPTH_TEST);
  355.         glBindTexture(GL_TEXTURE_2D, showSphereMap);
  356.         glBegin(GL_QUADS);
  357.             glTexCoord2f(0,0);
  358.             glVertex2f(0,0);
  359.             glTexCoord2f(1,0);
  360.             glVertex2f(1,0);
  361.             glTexCoord2f(1,1);
  362.             glVertex2f(1,1);
  363.             glTexCoord2f(0,1);
  364.             glVertex2f(0,1);
  365.         glEnd();
  366.     } else {
  367.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  368.  
  369.         glDisable(GL_TEXTURE_2D);
  370.         glEnable(GL_DEPTH_TEST);
  371.  
  372.         glMatrixMode(GL_PROJECTION);
  373.         glLoadIdentity();
  374.         gluPerspective(60.0, (GLfloat) W / (GLfloat) H, 0.5, 40.0);
  375.  
  376.         glMatrixMode(GL_MODELVIEW);
  377.         glLoadIdentity();
  378.         gluLookAt(eye[0], eye[1], eye[2],  /* eye at eye */
  379.                   0, 0, 0,  /* looking at object */
  380.                   up[0], up[1], up[2]);
  381.  
  382.         positionLights(-1, NULL);
  383.         drawView(-1, NULL);
  384.                 if (multipass) {
  385.                   doSphereMap = 1;
  386.                 }
  387.         drawObject(&sphere[0]);
  388.                 if (multipass) {
  389.                   doSphereMap = 0;
  390.                   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  391.                   glDepthFunc(GL_EQUAL);
  392.                   glEnable(GL_BLEND);
  393.           drawObject(&sphere[0]);
  394.                   glDisable(GL_BLEND);
  395.                   glDepthFunc(GL_LESS);
  396.                 }
  397.                 if (multipass) {
  398.                   doSphereMap = 1;
  399.                 }
  400.         drawObject(&sphere[1]);
  401.     }
  402.  
  403.     if (doubleBuffer) {
  404.         glutSwapBuffers();
  405.     }
  406. }
  407.  
  408. int angle = 0;
  409. int downx, downy;
  410.  
  411. void
  412. motion(int x, int y)
  413. {
  414.         if (moving) {
  415.     angle += (x - downx);
  416.     angle = angle % 360;
  417.     eye[0] = sin(angle * 3.14159/180.0) * -15;
  418.     eye[2] = cos(angle * 3.14159/180.0) * -15;
  419.     eye[1] = eye[1] + 0.1 * (y - downy);
  420.     if (eye[1] > +25) eye[1] = +25;
  421.     if (eye[1] < -25) eye[1] = -25;
  422.     smapSetEyeVector(smap, eye);
  423.     smapSetEyeVector(smap2, eye);
  424.     glutPostRedisplay();
  425.     downx = x;
  426.     downy = y;
  427.         }
  428. }
  429.  
  430. void
  431. mouse(int button, int state, int x, int y)
  432. {
  433.     if (button == GLUT_LEFT_BUTTON) {
  434.         if (state == GLUT_DOWN) {
  435.                         moving = 1;
  436.             downx = x;
  437.             downy = y;
  438.                 } else {
  439.                         moving = 0;
  440.                 }
  441.     }
  442. }
  443.  
  444. void
  445. idle(void)
  446. {
  447.    timeCount = timeCount + 0.1;
  448.    obj2[Y] = -5 + cos(timeCount) * 1.0;
  449.    smapSetObjectVector(smap, obj);
  450.    smapSetObjectVector(smap2, obj2);
  451.    smapSetObjectVector(smap3, obj);
  452.    glutPostRedisplay();
  453. }
  454.  
  455. /* When not visible, stop animating.  Restart when visible again. */
  456. static void 
  457. visible(int vis)
  458. {
  459.   if (vis == GLUT_VISIBLE) {
  460.     if (animation)
  461.       glutIdleFunc(idle);
  462.   } else {
  463.     if (!animation)
  464.       glutIdleFunc(NULL);
  465.   }
  466. }
  467.  
  468. void initGraphicsState(void);
  469. void initCallbacks(int ifFullscreen);
  470.  
  471. void
  472. menu(int value)
  473. {
  474.     switch (value) {
  475.     case 0:
  476.         showSphereMap = 0;
  477.         break;
  478.     case 1:
  479.         showSphereMap = TO_SPHERE_MAP;
  480.         break;
  481.     case -1:
  482.         showSphereMap = TO_SPHERE_MAP2;
  483.         break;
  484.     case -2:
  485.         showSphereMap = TO_SPHERE_MAP3;
  486.         break;
  487.     case 4:
  488.         object = (object + 1) % 3;
  489.         break;
  490.     case 5:
  491.         smapSetSphereMapTexDim(smap, 64);
  492.         smapSetViewTexDim(smap, 64);
  493.         smapSetSphereMapTexDim(smap2, 64);
  494.         smapSetViewTexDim(smap2, 64);
  495.         smapSetSphereMapTexDim(smap3, 64);
  496.         smapSetViewTexDim(smap3, 64);
  497.         break;
  498.     case 6:
  499.         smapSetSphereMapTexDim(smap, 128);
  500.         smapSetViewTexDim(smap, 128);
  501.         smapSetSphereMapTexDim(smap2, 128);
  502.         smapSetViewTexDim(smap2, 128);
  503.         smapSetSphereMapTexDim(smap3, 128);
  504.         smapSetViewTexDim(smap3, 128);
  505.             break;  
  506.     case 7:
  507.         smapSetSphereMapTexDim(smap, 256);
  508.         smapSetViewTexDim(smap, 256);
  509.         smapSetSphereMapTexDim(smap2, 256);
  510.         smapSetViewTexDim(smap2, 256);
  511.         smapSetSphereMapTexDim(smap3, 256);
  512.         smapSetViewTexDim(smap3, 256);
  513.         break;
  514.     case 8:
  515.         glDrawBuffer(GL_FRONT);
  516.         glReadBuffer(GL_FRONT);
  517.         doubleBuffer = 0;
  518.         break;
  519.     case 9:
  520.         glDrawBuffer(GL_BACK);
  521.         glReadBuffer(GL_BACK);
  522.         doubleBuffer = 1;
  523.         break;
  524.         case 10:
  525.                 dynamicSmap = 1;
  526.                 break;
  527.         case 11:
  528.                 dynamicSmap = 0;
  529.                 break;
  530.         case 12:
  531.                 cullBackFaces = 1;
  532.                 break;
  533.         case 13:
  534.                 cullBackFaces = 0;
  535.                 break;
  536.         case 14:
  537.                 doSphereMap = 1;
  538.                 multipass = 0;
  539.                 break;
  540.         case 15:
  541.                 doSphereMap = 0;
  542.                 multipass = 0;
  543.                 break;
  544.         case 16:
  545.                 multipass = 1;
  546.                 break;
  547.         case 17:
  548.                 animation = !animation;
  549.                 if (animation) {
  550.                         glutIdleFunc(idle);
  551.                 } else {
  552.                         glutIdleFunc(NULL);
  553.                 }
  554.                 break;
  555.     case 666:
  556.         exit(0);
  557.         break;
  558.     }
  559.     glutPostRedisplay();
  560. }
  561.  
  562. void
  563. keyboard(unsigned char c, int x, int y)
  564. {
  565.         switch (c) {
  566.         case 27:
  567.                 exit(0);
  568.                 break;
  569.         case '1':
  570.                 menu(5);
  571.                 break;
  572.         case '2':
  573.                 menu(6);
  574.                 break;
  575.         case '3':
  576.                 menu(7);
  577.                 break;
  578.         case 's':
  579.                 menu(4);
  580.                 break;
  581.         case 'f':
  582.                 glutGameModeString("400x300:16@60");
  583.                 glutEnterGameMode();
  584.                 initGraphicsState();
  585.                 initCallbacks(0);
  586.                 break;
  587.         case 'l':
  588.                 glutLeaveGameMode();
  589.                 glutSetWindow(win);
  590.                 break;
  591.         case 'a':
  592.                 sphereTess += 5;
  593.                 glutPostRedisplay();
  594.                 break;
  595.         case 'z':
  596.                 sphereTess -= 5;
  597.                 glutPostRedisplay();
  598.                 break;
  599.         case 'b':
  600.         case 'B':
  601.                 multibounce = !multibounce;
  602.                 printf("multibounce = %d\n", multibounce);
  603.                 break;
  604.         case ' ':
  605.                 menu(17);
  606.                 break;
  607.         }
  608. }
  609.  
  610. void
  611. initGraphicsState(void)
  612. {
  613.     GLfloat lightColor[4] = { 0.6, 0.6, 0.6, 1.0 };  /* white */
  614.  
  615.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  616.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
  617.     glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor);
  618.     glLightfv(GL_LIGHT2, GL_DIFFUSE, lightColor);
  619.     glLightfv(GL_LIGHT3, GL_DIFFUSE, lightColor);
  620.     glEnable(GL_LIGHTING);
  621.     glEnable(GL_LIGHT0);
  622.     glEnable(GL_LIGHT1);
  623.     glEnable(GL_LIGHT2);
  624.     glEnable(GL_LIGHT3);
  625.     glEnable(GL_DEPTH_TEST);
  626.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  627.     glEnable(GL_COLOR_MATERIAL);
  628.     glEnable(GL_NORMALIZE);
  629.  
  630.         glBindTexture(GL_TEXTURE_2D, 100);
  631.         makePatternTexture();
  632. }
  633.  
  634. void
  635. initCallbacks(int ifFullscreen)
  636. {
  637.         glutReshapeFunc(reshape);
  638.     glutDisplayFunc(display);
  639.         glutVisibilityFunc(visible);
  640.     glutMouseFunc(mouse);
  641.         glutKeyboardFunc(keyboard);
  642.     glutMotionFunc(motion);
  643.  
  644.         if (ifFullscreen) {
  645.  
  646.         glutCreateMenu(menu);
  647.     glutAddMenuEntry("eye view", 0);
  648.     glutAddMenuEntry("eye to obj 1 smap", 1);
  649.     glutAddMenuEntry("eye to obj 2 smap", -1);
  650.     glutAddMenuEntry("obj 1 to obj 2 smap", -2);
  651.     glutAddMenuEntry("switch object", 4);
  652.     glutAddMenuEntry("texdim = 64", 5);
  653.     glutAddMenuEntry("texdim = 128", 6);
  654.     glutAddMenuEntry("texdim = 256", 7);
  655.     glutAddMenuEntry("single buffer", 8);
  656.     glutAddMenuEntry("double buffer", 9);
  657.         glutAddMenuEntry("dynamic smap", 10);
  658.         glutAddMenuEntry("stop smap building", 11);
  659.         glutAddMenuEntry("cull back faces", 12);
  660.         glutAddMenuEntry("cull front faces", 13);
  661.         glutAddMenuEntry("sphere map", 14);
  662.         glutAddMenuEntry("texture", 15);
  663.         glutAddMenuEntry("multipass", 16);
  664.         glutAddMenuEntry("toggle animate", 17);
  665.     glutAddMenuEntry("quit", 666);
  666.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  667.  
  668.         }
  669. }
  670.  
  671. int
  672. main(int argc, char **argv)
  673. {
  674.     glutInit(&argc, argv);
  675.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  676.     win = glutCreateWindow("fakeraytrace");
  677.  
  678.     initGraphicsState();
  679.         initCallbacks(1);
  680.  
  681.     smap = smapCreateSphereMap(NULL);
  682.     smapSetSphereMapTexObj    (smap, TO_SPHERE_MAP);
  683.     smapSetViewTexObjs        (smap, texobjs);
  684.     smapSetEyeVector          (smap, eye);
  685.     smapSetUpVector           (smap, up);
  686.     smapSetObjectVector       (smap, obj);
  687.     smapSetNearFar            (smap, 0.5, 40.0);
  688.     smapSetPositionLightsFunc (smap, positionLights);
  689.     smapSetDrawViewFunc       (smap, drawView);
  690.     smapSetViewTexDim         (smap, 64);
  691.     smapSetSphereMapTexDim    (smap, 64);
  692.         smapSetContextData(smap, &sphere[0]);
  693.  
  694.         smap2 = smapCreateSphereMap(NULL);
  695.     smapSetSphereMapTexObj    (smap2, TO_SPHERE_MAP2);
  696.     smapSetViewTexObjs        (smap2, texobjs2);
  697.     smapSetEyeVector          (smap2, eye);
  698.     smapSetUpVector           (smap2, up);
  699.     smapSetObjectVector       (smap2, obj2);
  700.     smapSetNearFar            (smap2, 0.5, 40.0);
  701.     smapSetPositionLightsFunc (smap2, positionLights);
  702.     smapSetDrawViewFunc       (smap2, drawView);
  703.     smapSetViewTexDim         (smap2, 64);
  704.     smapSetSphereMapTexDim    (smap2, 64);
  705.         smapSetContextData(smap2, &sphere[1]);
  706.  
  707.         smap3 = smapCreateSphereMap(NULL);
  708.     smapSetSphereMapTexObj    (smap3, TO_SPHERE_MAP3);
  709.     smapSetViewTexObjs        (smap3, texobjs3);
  710.     smapSetEyeVector          (smap3, obj2);
  711.     smapSetUpVector           (smap3, up2);
  712.     smapSetObjectVector       (smap3, obj);
  713.     smapSetNearFar            (smap3, 0.1, 40.0);
  714.     smapSetPositionLightsFunc (smap3, positionLights);
  715.     smapSetDrawViewFunc       (smap3, drawView);
  716.     smapSetViewTexDim         (smap3, 64);
  717.     smapSetSphereMapTexDim    (smap3, 64);
  718.         smapSetContextData(smap3, &sphere[2]);
  719.  
  720.     glutMainLoop();
  721.     return 0;
  722. }
  723.